package spring.security.core.util;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;
import java.util.UUID;

public class RandomUtils {
	
	public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static final String LETTERCHAR = "abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static final String NUMBERCHAR = "0123456789";

	public static int integer(int scopeMin, int scoeMax) {
		Random random = new Random();
		return random.nextInt(scoeMax) % (scoeMax - scopeMin + 1) + scopeMin;
	}

	public static String number(int length) {
		StringBuffer sb = new StringBuffer();
		Random random = new Random();

		for (int i = 0; i < length; ++i) {
			sb.append("0123456789".charAt(random.nextInt("0123456789".length())));
		}

		return sb.toString();
	}

	public static String String(int length) {
		StringBuffer sb = new StringBuffer();
		Random random = new Random();

		for (int i = 0; i < length; ++i) {
			sb.append("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
					.charAt(random.nextInt("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".length())));
		}

		return sb.toString();
	}

	public static String MixString(int length) {
		StringBuffer sb = new StringBuffer();
		Random random = new Random();

		for (int i = 0; i < length; ++i) {
			sb.append("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
					.charAt(random.nextInt("abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".length())));
		}

		return sb.toString();
	}

	public static String LowerString(int length) {
		return MixString(length).toLowerCase();
	}

	public static String UpperString(int length) {
		return MixString(length).toUpperCase();
	}

	public static String ZeroString(int length) {
		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < length; ++i) {
			sb.append('0');
		}

		return sb.toString();
	}

	public static String toFixdLengthString(long num, int fixdlenth) {
		StringBuffer sb = new StringBuffer();
		String strNum = String.valueOf(num);
		if (fixdlenth - strNum.length() >= 0) {
			sb.append(ZeroString(fixdlenth - strNum.length()));
			sb.append(strNum);
			return sb.toString();
		} else {
			throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth + "的字符串发生异常！");
		}
	}

	public static String toFixdLengthString(int num, int fixdlenth) {
		StringBuffer sb = new StringBuffer();
		String strNum = String.valueOf(num);
		if (fixdlenth - strNum.length() >= 0) {
			sb.append(ZeroString(fixdlenth - strNum.length()));
			sb.append(strNum);
			return sb.toString();
		} else {
			throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth + "的字符串发生异常！");
		}
	}

	public static int getNotSimple(int[] param, int len) {
		Random rand = new Random();

		int result;
		int i;
		for (result = param.length; result > 1; --result) {
			i = rand.nextInt(result);
			int tmp = param[i];
			param[i] = param[result - 1];
			param[result - 1] = tmp;
		}

		result = 0;

		for (i = 0; i < len; ++i) {
			result = result * 10 + param[i];
		}

		return result;
	}

	public static <T> T randomItem(T[] param) {
		int index = integer(0, param.length);
		return param[index];
	}

	private static String strMultiplication(String str, int multiplication) {
		StringBuffer buffer = new StringBuffer();

		for (int i = 0; i < multiplication; ++i) {
			buffer.append(str);
		}

		return buffer.toString();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static <T> T randomItem(T[] param, double[] percentum) {
		int length = percentum.length;
		Integer[] ints = new Integer[percentum.length];

		int max;
		int sum;
		for (max = 0; max < percentum.length; ++max) {
			String arr = String.valueOf(percentum[max]);
			sum = arr.indexOf(".");
			if (sum > -1) {
				ints[max] = Integer.valueOf(arr.length() - max - 1);
			} else {
				ints[max] = Integer.valueOf(0);
			}
		}

		max = ((Integer) Collections.max(Arrays.asList(ints))).intValue();
		int[] arg12 = new int[length];
		sum = 0;
		HashMap map = new HashMap(length);
		int multiple = Integer.parseInt("1" + strMultiplication("0", max));

		int indexSum;
		int index;
		for (indexSum = 0; indexSum < length; ++indexSum) {
			index = (int) (percentum[indexSum] * (double) multiple);
			arg12[indexSum] = index;
			if (indexSum == 0) {
				map.put(Integer.valueOf(indexSum), new Integer[] { Integer.valueOf(1), Integer.valueOf(index) });
			} else {
				map.put(Integer.valueOf(indexSum),
						new Integer[] { Integer.valueOf(sum), Integer.valueOf(sum + index) });
			}

			sum += index;
		}

		indexSum = integer(1, sum);
		index = -1;

		for (int i = 0; i < length; ++i) {
			Integer[] scope = (Integer[]) ((Integer[]) map.get(Integer.valueOf(i)));
			if (indexSum == 1) {
				index = 0;
				break;
			}

			if (indexSum > scope[0].intValue() && indexSum <= scope[1].intValue()) {
				index = i;
				break;
			}
		}

		if (index == -1) {
			throw new RuntimeException("随机失败");
		} else {
			return param[index];
		}
	}

	public static String uuid() {
		UUID uuid = UUID.randomUUID();
		String s = uuid.toString();
		return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
	}

	public static String UUID() {
		UUID uuid = UUID.randomUUID();
		String s = uuid.toString();
		String temp = s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23)
				+ s.substring(24);
		return temp.toUpperCase();
	}

	public static String squid(String hostId) {
		Long date = Long.valueOf((new Date()).getTime());
		String s = UUID.randomUUID().toString();
		String str = Long.toHexString(date.longValue());
		String result = str + hostId + s.substring(17, 18) + s.substring(19, 23) + s.substring(24);
		return result.toUpperCase();
	}
	
}
